//
//  EFGoodCountViewCell.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/25.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFGoodCountViewCell.h"
#import "EFGoodCountViewItem.h"

@interface EFGoodCountViewCell()


@property(nonatomic, strong)UILabel *titleLabel;


@property(nonatomic, strong)UIButton *addButton;
@property(nonatomic, strong)UIButton *reduceButton;
@property(nonatomic, strong)UILabel *countLabel;
@property(nonatomic, strong)UIView *bottomLine;

@end

@implementation EFGoodCountViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor whiteColor];
        [self initView];
    }
    return self;
}

-(void)setObject:(EFGoodCountViewItem *)item{
    [self.countLabel setText:@(item.count).stringValue];
    [self.countLabel sizeToFit];
    if (self.countLabel.width < 10) {
        self.countLabel.width = 10.f;
    }
    [self refreshCountUI];
}

-(void)initView{
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.reduceButton];
    [self.contentView addSubview:self.countLabel];
    [self.contentView addSubview:self.addButton];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.titleLabel.left = 20;
    self.titleLabel.top = 16;
    
    [self refreshCountUI];
    
    self.bottomLine.top = self.titleLabel.bottom + 18;
    self.bottomLine.left = 20;
    
} 

- (void)onClickReduceBtn{
    NSInteger count = self.countLabel.text.integerValue;
    count --;
    if (count <= 0) {
        count = 0;
    }
    self.countLabel.text = @(count).stringValue;
    [self.countLabel sizeToFit];
    if (self.countLabel.width < 10) {
        self.countLabel.width = 10.f;
    }
    [self refreshCountUI];
    if(self.changeblock){
        self.changeblock(count);
    }
}

- (void)onClickAddBtn{
    NSInteger count = self.countLabel.text.integerValue;
    count ++;
    self.countLabel.text = @(count).stringValue;
    [self.countLabel sizeToFit];
    if (self.countLabel.width < 10) {
        self.countLabel.width = 10.f;
    }
    [self refreshCountUI];
    if(self.changeblock){
        self.changeblock(count);
    }
}

- (void)refreshCountUI{
    self.addButton.left = kScreenWidth - self.addButton.width - 20;
    self.addButton.top = 17;
    self.countLabel.right = self.addButton.left - scale(10.f);
    self.countLabel.centerY = self.addButton.centerY;
    self.reduceButton.right = self.countLabel.left - scale(10.f);
    self.reduceButton.bottom = self.addButton.bottom;
}


- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 54, 20)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.font = [UIFont systemFontOfSize:13.f];
        _titleLabel.textColor = kBlack999;
        [_titleLabel setText:@"购买数量"];
        [_titleLabel sizeToFit];
    }
    return _titleLabel;
}

- (UILabel *)countLabel{
    if (!_countLabel) {
        _countLabel = [[UILabel alloc] init];
        _countLabel.backgroundColor = [UIColor clearColor];
        _countLabel.font = [UIFont systemFontOfSize:13.f];
        _countLabel.textColor = kBlack333;
    }
    return _countLabel;
}

- (UIButton *)reduceButton{
    if (!_reduceButton) {
        _reduceButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 18.f, 19.f)];
        [_reduceButton setImage:IMG(@"cutback_icon_default") forState:UIControlStateNormal];
        [_reduceButton addTarget:self action:@selector(onClickReduceBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _reduceButton;
}

- (UIButton *)addButton{
    if (!_addButton) {
        _addButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20.f, 19.f)];
        [_addButton setImage:IMG(@"buy_icon_home_default") forState:UIControlStateNormal];
        [_addButton addTarget:self action:@selector(onClickAddBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}


- (UIView *)bottomLine{
    if (!_bottomLine) {
        _bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - 40, 1)];
        _bottomLine.backgroundColor = RGBCOLOR(0xECECEC);
    }
    return _bottomLine;
}


@end
